1 Imports System.Data.SqlClient
2 Imports System.IO
3
4 Public Class frmProduct
5 Private Function GenerateID() As String
6 con = New SqlConnection(cs)
7 Dim value As String = "0000"
8 Try
9 ' Fetch the latest ID from the database
10 con.Open()
11 cmd = New SqlCommand("SELECT TOP 1 PID FROM Product ORDER BY PID DESC", con)
12 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
13 If rdr.HasRows Then
14 rdr.Read()
15 value = rdr.Item("PID")
16 End If
17 rdr.Close()
18 ' Increase the ID by 1
19 value += 1
20 ' Because incrementing a string with an integer removes 0's
21 ' we need to replace them. If necessary.
22 If value <= 9 Then 'Value is between 0 and 10
23 value = "000" & value
24 ElseIf value <= 99 Then 'Value is between 9 and 100
25 value = "00" & value
26 ElseIf value <= 999 Then 'Value is between 999 and 1000
27 value = "0" & value
28 End If
29 Catch ex As Exception
30 ' If an error occurs, check the connection state and close it if necessary.
31 If con.State = ConnectionState.Open Then
32 con.Close()
33 End If
34 value = "0000"
35 End Try
36 Return value
37 End Function
38 Sub auto()
39 Try
40 txtID.Text = GenerateID()
41 txtProductCode.Text = "P-" + GenerateID()
42 Catch ex As Exception
43 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
44 End Try
45 End Sub
46 Sub Reset()
47 txtCostPrice.Text = ""
48 txtProductCode.Text = ""
49 txtDiscount.Text = 0.0
50 txtSellingPrice.Text = ""
51 txtVAT.Text = 0.0
52 txtReorderPoint.Text = ""
53 txtFeatures.Text = ""
54 txtProductName.Text = ""
55 cmbCategory.SelectedIndex = -1
56 cmbSubCategory.SelectedIndex = -1
57 cmbSubCategory.Enabled = False
58 txtProductCode.Focus()
59 btnSave.Enabled = True
60 btnUpdate.Enabled = False
61 btnDelete.Enabled = False
62 Picture.Image = My.Resources._12
63 dgw.Rows.Clear()
64 btnRemove.Enabled = False
65 auto()
66 End Sub
67 Sub fillCategory()
68 Try
69 con = New SqlConnection(cs)
70 con.Open()
71 adp = New SqlDataAdapter()
72 adp.SelectCommand = New SqlCommand("SELECT distinct RTRIM(CategoryName) FROM Category", con)
73 ds = New DataSet("ds")
74 adp.Fill(ds)
75 dtable = ds.Tables(0)
76 cmbCategory.Items.Clear()
77 For Each drow As DataRow In dtable.Rows
78 cmbCategory.Items.Add(drow(0).ToString())
79 Next
80 Catch ex As Exception
81 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
82 End Try
83 End Sub
84
85 Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
86 Me.Close()
87 End Sub
88
89
90 Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
91 If Len(Trim(txtProductCode.Text)) = 0 Then
92 MessageBox.Show("Please enter charges quote", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
93 txtProductCode.Focus()
94 Exit Sub
95 End If
96 If Len(Trim(txtProductName.Text)) = 0 Then
97 MessageBox.Show("Please enter upfront", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
98 txtProductName.Focus()
99 Exit Sub
100 End If
101 If Len(Trim(cmbCategory.Text)) = 0 Then
102 MessageBox.Show("Please select category", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
103 cmbCategory.Focus()
104 Exit Sub
105 End If
106 If Len(Trim(cmbSubCategory.Text)) = 0 Then
107 MessageBox.Show("Please select sub category", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
108 cmbSubCategory.Focus()
109 Exit Sub
110 End If
111
112 If Len(Trim(txtCostPrice.Text)) = 0 Then
113 MessageBox.Show("Please enter cost price", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
114 txtCostPrice.Focus()
115 Exit Sub
116 End If
117 If Len(Trim(txtDiscount.Text)) = 0 Then
118 MessageBox.Show("Please enter discount", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
119 txtDiscount.Focus()
120 Exit Sub
121 End If
122 If Len(Trim(txtSellingPrice.Text)) = 0 Then
123 MessageBox.Show("Please enter selling price", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
124 txtSellingPrice.Focus()
125 Exit Sub
126 End If
127 If Len(Trim(txtVAT.Text)) = 0 Then
128 MessageBox.Show("Please enter VAT", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
129 txtVAT.Focus()
130 Exit Sub
131 End If
132 If Len(Trim(txtReorderPoint.Text)) = 0 Then
133 MessageBox.Show("Please enter reorder point", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
134 txtReorderPoint.Focus()
135 Exit Sub
136 End If
137 If dgw.Rows.Count = 0 Then
138 MessageBox.Show("Please add product images in datagridview", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
139 Exit Sub
140 End If
141 Try
142 Fill()
143 con = New SqlConnection(cs)
144 con.Open()
145 Dim cb As String = "insert into Product(PID,ProductCode, Productname, SubCategoryID, Description, CostPrice, SellingPrice, Discount, VAT, ReorderPoint) VALUES (" & txtID.Text & ",@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9)"
146 cmd = New SqlCommand(cb)
147 cmd.Parameters.AddWithValue("@d1", txtProductCode.Text)
148 cmd.Parameters.AddWithValue("@d2", txtProductName.Text)
149 cmd.Parameters.AddWithValue("@d3", txtSubCategoryID.Text)
150 cmd.Parameters.AddWithValue("@d4", txtFeatures.Text)
151 cmd.Parameters.AddWithValue("@d5", txtCostPrice.Text)
152 cmd.Parameters.AddWithValue("@d6", txtSellingPrice.Text)
153 cmd.Parameters.AddWithValue("@d7", txtDiscount.Text)
154 cmd.Parameters.AddWithValue("@d8", txtVAT.Text)
155 cmd.Parameters.AddWithValue("@d9", txtReorderPoint.Text)
156 cmd.Connection = con
157 cmd.ExecuteNonQuery()
158 con.Close()
159 con = New SqlConnection(cs)
160 con.Open()
161 Dim ck As String = "insert into Product_Join(ProductID,photo) VALUES (" & txtID.Text & ",@d2)"
162 cmd = New SqlCommand(ck)
163 cmd.Connection = con
164 ' Prepare command for repeated execution
165 cmd.Prepare()
166 ' Data to be inserted
167 For Each row As DataGridViewRow In dgw.Rows
168 If Not row.IsNewRow Then
169 Dim ms As New MemoryStream()
170 Dim img As Image = row.Cells(0).Value
171 Dim bmpImage As New Bitmap(img)
172 bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
173 Dim data As Byte() = ms.GetBuffer()
174 Dim p As New SqlParameter("@d2", SqlDbType.Image)
175 p.Value = data
176 cmd.Parameters.Add(p)
177 cmd.ExecuteNonQuery()
178 cmd.Parameters.Clear()
179 End If
180 Next
181 con.Close()
182 LogFunc(lblUser.Text, "added the new Product '" & txtProductName.Text & "' having Product code '" & txtProductCode.Text & "'")
183 RefreshRecords()
184 MessageBox.Show("Successfully saved", "Product Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
185 auto()
186 con.Close()
187 Catch ex As Exception
188 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
189 End Try
190 End Sub
191
192 Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
193 Try
194 If MessageBox.Show("Do you really want to delete this record?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
195 DeleteRecord()
196 RefreshRecords()
197 End If
198 Catch ex As Exception
199 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
200 End Try
201 End Sub
202 Private Sub DeleteRecord()
203 Try
204 Dim RowsAffected As Integer = 0
205 con = New SqlConnection(cs)
206 con.Open()
207 Dim cl2 As String = "SELECT PID FROM Product INNER JOIN Stock_Product ON Product.PID = Stock_Product.ProductID where PID=@d1"
208 cmd = New SqlCommand(cl2)
209 cmd.Connection = con
210 cmd.Parameters.AddWithValue("@d1", txtID.Text)
211 rdr = cmd.ExecuteReader()
212 If rdr.Read Then
213 MessageBox.Show("Unable to delete..Already in use in Stock Entry", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
214 If Not rdr Is Nothing Then
215 rdr.Close()
216 End If
217 Exit Sub
218 End If
219 con.Close()
220 con = New SqlConnection(cs)
221 con.Open()
222 Dim cl As String = "SELECT PID FROM Product INNER JOIN Invoice_Product ON Product.PID = Invoice_Product.ProductID where PID=@d1"
223 cmd = New SqlCommand(cl)
224 cmd.Connection = con
225 cmd.Parameters.AddWithValue("@d1", txtID.Text)
226 rdr = cmd.ExecuteReader()
227 If rdr.Read Then
228 MessageBox.Show("Unable to delete..Already in use in Billing", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
229 If Not rdr Is Nothing Then
230 rdr.Close()
231 End If
232 Exit Sub
233 End If
234 con.Close()
235 con = New SqlConnection(cs)
236 con.Open()
237 Dim cl1 As String = "SELECT PID FROM Product INNER JOIN Quotation_Join ON Product.PID = Quotation_Join.ProductID where PID=@d1"
238 cmd = New SqlCommand(cl1)
239 cmd.Connection = con
240 cmd.Parameters.AddWithValue("@d1", txtID.Text)
241 rdr = cmd.ExecuteReader()
242 If rdr.Read Then
243 MessageBox.Show("Unable to delete..Already in use in Quotation", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
244 If Not rdr Is Nothing Then
245 rdr.Close()
246 End If
247 Exit Sub
248 End If
249 con.Close()
250 con = New SqlConnection(cs)
251 con.Open()
252 Dim cl3 As String = "SELECT PID FROM Product INNER JOIN Invoice1_Product ON Product.PID = Invoice1_Product.ProductID where PID=@d1"
253 cmd = New SqlCommand(cl3)
254 cmd.Connection = con
255 cmd.Parameters.AddWithValue("@d1", txtID.Text)
256 rdr = cmd.ExecuteReader()
257 If rdr.Read Then
258 MessageBox.Show("Unable to delete..Already in use in Billing", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
259 If Not rdr Is Nothing Then
260 rdr.Close()
261 End If
262 Exit Sub
263 End If
264 con.Close()
265 con = New SqlConnection(cs)
266 con.Open()
267 Dim cq As String = "delete from Product where PID=@d1"
268 cmd = New SqlCommand(cq)
269 cmd.Parameters.AddWithValue("@d1", txtID.Text)
270 cmd.Connection = con
271 RowsAffected = cmd.ExecuteNonQuery()
272 If RowsAffected > 0 Then
273 LogFunc(lblUser.Text, "deleted the Product '" & txtProductName.Text & "' having Product code '" & txtProductCode.Text & "'")
274 MessageBox.Show("Successfully deleted", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
275 Reset()
276 Else
277 MessageBox.Show("No record found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information)
278 Reset()
279 If con.State = ConnectionState.Open Then
280 con.Close()
281 End If
282 con.Close()
283 End If
284 Catch ex As Exception
285 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
286 End Try
287 End Sub
288
289 Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
290 If Len(Trim(txtProductCode.Text)) = 0 Then
291 MessageBox.Show("Please enter product code", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
292 txtProductCode.Focus()
293 Exit Sub
294 End If
295 If Len(Trim(txtProductName.Text)) = 0 Then
296 MessageBox.Show("Please enter product name", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
297 txtProductName.Focus()
298 Exit Sub
299 End If
300 If Len(Trim(cmbCategory.Text)) = 0 Then
301 MessageBox.Show("Please select category", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
302 cmbCategory.Focus()
303 Exit Sub
304 End If
305 If Len(Trim(cmbSubCategory.Text)) = 0 Then
306 MessageBox.Show("Please select sub category", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
307 cmbSubCategory.Focus()
308 Exit Sub
309 End If
310
311 If Len(Trim(txtCostPrice.Text)) = 0 Then
312 MessageBox.Show("Please enter cost price", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
313 txtCostPrice.Focus()
314 Exit Sub
315 End If
316 If Len(Trim(txtDiscount.Text)) = 0 Then
317 MessageBox.Show("Please enter discount", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
318 txtDiscount.Focus()
319 Exit Sub
320 End If
321 If Len(Trim(txtSellingPrice.Text)) = 0 Then
322 MessageBox.Show("Please enter selling price", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
323 txtSellingPrice.Focus()
324 Exit Sub
325 End If
326 If Len(Trim(txtVAT.Text)) = 0 Then
327 MessageBox.Show("Please enter VAT", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
328 txtVAT.Focus()
329 Exit Sub
330 End If
331 If Len(Trim(txtReorderPoint.Text)) = 0 Then
332 MessageBox.Show("Please enter reorder point", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
333 txtReorderPoint.Focus()
334 Exit Sub
335 End If
336 If dgw.Rows.Count = 0 Then
337 MessageBox.Show("Please add product images in datagridview", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
338 Exit Sub
339 End If
340 Try
341 Fill()
342 con = New SqlConnection(cs)
343 con.Open()
344 Dim cb As String = "Update Product set Productname=@d2, SubCategoryID=@d3, Description=@d4, CostPrice=@d5, SellingPrice=@d6, Discount=@d7, VAT=@d8, ReorderPoint=@d9,ProductCode=@d1 where PID=" & txtID.Text & ""
345 cmd = New SqlCommand(cb)
346 cmd.Parameters.AddWithValue("@d2", txtProductName.Text)
347 cmd.Parameters.AddWithValue("@d3", txtSubCategoryID.Text)
348 cmd.Parameters.AddWithValue("@d4", txtFeatures.Text)
349 cmd.Parameters.AddWithValue("@d5", txtCostPrice.Text)
350 cmd.Parameters.AddWithValue("@d6", txtSellingPrice.Text)
351 cmd.Parameters.AddWithValue("@d7", txtDiscount.Text)
352 cmd.Parameters.AddWithValue("@d8", txtVAT.Text)
353 cmd.Parameters.AddWithValue("@d9", txtReorderPoint.Text)
354 cmd.Parameters.AddWithValue("@d1", txtProductCode.Text)
355 cmd.Connection = con
356 cmd.ExecuteNonQuery()
357 con.Close()
358 con = New SqlConnection(cs)
359 con.Open()
360 Dim cb1 As String = "delete from Product_Join where ProductID=@d1"
361 cmd = New SqlCommand(cb1)
362 cmd.Parameters.AddWithValue("@d1", txtID.Text)
363 cmd.Connection = con
364 cmd.ExecuteReader()
365 con.Close()
366 con = New SqlConnection(cs)
367 con.Open()
368 Dim ck As String = "insert into Product_Join(ProductID,Photo) VALUES (" & txtID.Text & ",@d2)"
369 cmd = New SqlCommand(ck)
370 cmd.Connection = con
371 ' Prepare command for repeated execution
372 cmd.Prepare()
373 ' Data to be inserted
374 For Each row As DataGridViewRow In dgw.Rows
375 If Not row.IsNewRow Then
376 Dim ms As New MemoryStream()
377 Dim img As Image = row.Cells(0).Value
378 Dim bmpImage As New Bitmap(img)
379 bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
380 Dim data As Byte() = ms.GetBuffer()
381 Dim p As New SqlParameter("@d2", SqlDbType.Image)
382 p.Value = data
383 cmd.Parameters.Add(p)
384 cmd.ExecuteNonQuery()
385 cmd.Parameters.Clear()
386 End If
387 Next
388 con.Close()
389 LogFunc(lblUser.Text, "updated the Product '" & txtProductName.Text & "' having Product code '" & txtProductCode.Text & "'")
390 RefreshRecords()
391 MessageBox.Show("Successfully updated", "Product Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
392 btnUpdate.Enabled = False
393 con.Close()
394 Catch ex As Exception
395 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
396 End Try
397 End Sub
398
399 Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
400 Reset()
401 End Sub
402
403 Private Sub Browse_Click(sender As System.Object, e As System.EventArgs) Handles Browse.Click
404 Try
405 With OpenFileDialog1
406 .Filter = ("Images |*.png; *.bmp; *.jpg;*.jpeg; *.gif;")
407 .FilterIndex = 4
408 End With
409 'Clear the file name
410 OpenFileDialog1.FileName = ""
411 If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
412 Picture.Image = Image.FromFile(OpenFileDialog1.FileName)
413 End If
414 Catch ex As Exception
415 MsgBox(ex.ToString())
416 End Try
417 End Sub
418
419 Private Sub BRemove_Click(sender As System.Object, e As System.EventArgs) Handles BRemove.Click
420 Picture.Image = My.Resources._12
421 End Sub
422
423 Private Sub btnGetData_Click(sender As System.Object, e As System.EventArgs) Handles btnGetData.Click
424 Dim frm As New frmProductRecord
425 frm.lblSet.Text = "Product Entry"
426 frm.Reset()
427 frm.ShowDialog()
428 End Sub
429 Sub Fill()
430 Try
431 con = New SqlConnection(cs)
432 con.Open()
433 cmd = con.CreateCommand()
434 cmd.CommandText = "SELECT ID from SubCategory where Category=@d1 and SubCategoryName=@d2"
435 cmd.Parameters.AddWithValue("@d1", cmbCategory.Text)
436 cmd.Parameters.AddWithValue("@d2", cmbSubCategory.Text)
437 rdr = cmd.ExecuteReader()
438 If rdr.Read() Then
439 txtSubCategoryID.Text = rdr.GetValue(0)
440 End If
441 If (rdr IsNot Nothing) Then
442 rdr.Close()
443 End If
444 If con.State = ConnectionState.Open Then
445 con.Close()
446 End If
447 Catch ex As Exception
448 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
449 End Try
450 End Sub
451 Private Sub frmProduct_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
452 fillCategory()
453 End Sub
454
455 Private Sub cmbCategory_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbCategory.SelectedIndexChanged
456 Try
457 cmbSubCategory.Enabled = True
458 con = New SqlConnection(cs)
459 con.Open()
460 Dim ct As String = "SELECT distinct RTRIM(SubCategoryName) FROM SubCategory,Category where SubCategory.Category=Category.CategoryName and CategoryName=@d1"
461 cmd = New SqlCommand(ct)
462 cmd.Connection = con
463 cmd.Parameters.AddWithValue("@d1", cmbCategory.Text)
464 rdr = cmd.ExecuteReader()
465 cmbSubCategory.Items.Clear()
466 While rdr.Read
467 cmbSubCategory.Items.Add(rdr(0))
468 End While
469 con.Close()
470 Catch ex As Exception
471 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
472 End Try
473 End Sub
474
475 Private Sub txtPrice_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtCostPrice.KeyPress
476 Dim keyChar = e.KeyChar
477
478 If Char.IsControl(keyChar) Then
479 'Allow all control characters.
480 ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
481 Dim text = Me.txtCostPrice.Text
482 Dim selectionStart = Me.txtCostPrice.SelectionStart
483 Dim selectionLength = Me.txtCostPrice.SelectionLength
484
485 text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)
486
487 If Integer.TryParse(text, New Integer) AndAlso text.Length > 16 Then
488 'Reject an integer that is longer than 16 digits.
489 e.Handled = True
490 ElseIf Double.TryParse(text, New Double) AndAlso text.IndexOf("."c) < text.Length - 3 Then
491 'Reject a real number with two many decimal places.
492 e.Handled = False
493 End If
494 Else
495 'Reject all other characters.
496 e.Handled = True
497 End If
498 End Sub
499
500 Private Sub txtReorderPoint_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtReorderPoint.KeyPress
501 If (e.KeyChar < Chr(48) Or e.KeyChar > Chr(57)) And e.KeyChar <> Chr(8) Then
502 e.Handled = True
503 End If
504 End Sub
505
506 Private Sub txtDiscount_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtDiscount.KeyPress
507 Dim keyChar = e.KeyChar
508
509 If Char.IsControl(keyChar) Then
510 'Allow all control characters.
511 ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
512 Dim text = Me.txtDiscount.Text
513 Dim selectionStart = Me.txtDiscount.SelectionStart
514 Dim selectionLength = Me.txtDiscount.SelectionLength
515
516 text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)
517
518 If Integer.TryParse(text, New Integer) AndAlso text.Length > 16 Then
519 'Reject an integer that is longer than 16 digits.
520 e.Handled = True
521 ElseIf Double.TryParse(text, New Double) AndAlso text.IndexOf("."c) < text.Length - 3 Then
522 'Reject a real number with two many decimal places.
523 e.Handled = False
524 End If
525 Else
526 'Reject all other characters.
527 e.Handled = True
528 End If
529 End Sub
530
531 Private Sub txtVAT_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtVAT.KeyPress
532 Dim keyChar = e.KeyChar
533
534 If Char.IsControl(keyChar) Then
535 'Allow all control characters.
536 ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
537 Dim text = Me.txtVAT.Text
538 Dim selectionStart = Me.txtVAT.SelectionStart
539 Dim selectionLength = Me.txtVAT.SelectionLength
540
541 text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)
542
543 If Integer.TryParse(text, New Integer) AndAlso text.Length > 16 Then
544 'Reject an integer that is longer than 16 digits.
545 e.Handled = True
546 ElseIf Double.TryParse(text, New Double) AndAlso text.IndexOf("."c) < text.Length - 3 Then
547 'Reject a real number with two many decimal places.
548 e.Handled = False
549 End If
550 Else
551 'Reject all other characters.
552 e.Handled = True
553 End If
554 End Sub
555
556 Private Sub txtSellingPrice_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtSellingPrice.KeyPress
557 Dim keyChar = e.KeyChar
558
559 If Char.IsControl(keyChar) Then
560 'Allow all control characters.
561 ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
562 Dim text = Me.txtSellingPrice.Text
563 Dim selectionStart = Me.txtSellingPrice.SelectionStart
564 Dim selectionLength = Me.txtSellingPrice.SelectionLength
565
566 text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)
567
568 If Integer.TryParse(text, New Integer) AndAlso text.Length > 16 Then
569 'Reject an integer that is longer than 16 digits.
570 e.Handled = True
571 ElseIf Double.TryParse(text, New Double) AndAlso text.IndexOf("."c) < text.Length - 3 Then
572 'Reject a real number with two many decimal places.
573 e.Handled = False
574 End If
575 Else
576 'Reject all other characters.
577 e.Handled = True
578 End If
579 End Sub
580
581 Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
582 dgw.Rows.Add(Picture.Image)
583 End Sub
584
585 Private Sub btnRemove_Click(sender As System.Object, e As System.EventArgs) Handles btnRemove.Click
586 Try
587 For Each row As DataGridViewRow In dgw.SelectedRows
588 dgw.Rows.Remove(row)
589 Next
590 btnRemove.Enabled = False
591 Catch ex As Exception
592 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
593 End Try
594 End Sub
595
596 Private Sub dgw_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles dgw.MouseClick
597 If dgw.Rows.Count > 0 Then
598 btnRemove.Enabled = True
599 End If
600 End Sub
601 End Class